import React from 'react';
import {Router, Route, Switch} from 'react-router-dom';
import {Button} from 'antd-mobile'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import {actions} from '../reducers'
import { actions as UserAction } from '../reducers/user'

import history from '../history';

import { wechatShareHOC } from '../components/wechat-share'
import wxshare from '../components/wx-share'


import Login from '../pages/login'
import Register from '../pages/register'
import Home from '../pages/home'
import MyProject from '../pages/my_project'
import ProjectDetail from '../pages/project_detail'
import Personal from '../pages/my'
import Notification from '../pages/my/notification'
import Contacts from '../pages/my/contacts'
import MyCategory from '../pages/my/category'

import ProjectProfit from '../pages/my/project_profit'
import Invite from '../pages/my/invite'
import Point from '../pages/my/point'
import Score from '../pages/my/score'
import Role from '../pages/my/role'
import A_Apply from '../pages/my/role/apply/a'
import B_Apply from '../pages/my/role/apply/b'
import PM_Apply from '../pages/my/role/apply/pm'

import Authentication from '../pages/my/authentication'
import ExpertApply from '../pages/my/authentication/apply'
import Search from '../pages/search'

import NewProject from '../pages/new_project'
import Assign from '../pages/assign'
import AddAssign from '../pages/assign/add'

import AddSchedule from '../pages/project_detail/add_schedule'
import SetMileStone from '../pages/project_detail/set_milestone'

import NewRevision from '../pages/revision'

import Milestone from '../pages/milestone'

import Category from '../pages/category'

import Profile from '../pages/my/profile'

import WXToken from '../pages/token'
import DownloadPage from '../pages/download'

import PublicDetail from '../pages/public_detail'


class App extends React.Component {
    render() {
        return(
        <Router history={history}>
            <Switch>
                <Route path="/login" component={Login}/>
                <Route path="/register" component={Register}/>
                <Route exact path="/wxtoken" component={WXToken} />
                <Route exact path="/download" component={DownloadPage} />
                <WrapSwitch 
                    shouldRender={this.props.shouldRender} 
                    user_auth={this.props.user_auth}
                    userInfo={this.props.userInfo}
                />
            </Switch>
        </Router>   
        )
    }
}

class WrapSwitch extends React.Component {
    constructor(props) {
        super(props)
    }
    componentDidMount() {
        this.props.user_auth()
    }
    componentWillReceiveProps(nextProps) {
        if (this.props.userInfo.id==undefined && nextProps.userInfo.id) {
            let share = {
                title: '标题5454', // 分享标题
                desc: '描述4444！', // 分享描述
                link: location.origin+`?invite_id=${nextProps.userInfo.id}`, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: location.origin + '/lips_share.jpg', // 分享图标
            }
            wxshare(share)
            
            history.listen((location, action) => {
                // console.log(action, location.pathname, location.state)
                if ( action == 'PUSH' && ! /(\/login)|(\/register[\/\w+]?)/.test(location.pathname)) {
                    wxshare(share)
                }
            })
        }
    }
    render() {
        if (!this.props.shouldRender) {
            return null;
        }
        
        return (
            <React.Fragment>
                <Route exact path="/" component={Home}/>
                <Route path="/archive/:category_id" component={Category}/>
                <Route path="/personal" component={Personal}/>
                <Route path="/my-project" component={MyProject}/>
                <Route path='/project-detail' component={ProjectDetail}/>
                <Route path='/notification' component={Notification}/>
                <Route path='/contacts' component={Contacts}/>
                <Route path='/my-category' component={MyCategory}/>
                <Route path='/project-profit' component={ProjectProfit}/>
                <Route path='/invite' component={Invite}/>
                <Route path='/point' component={Point}/>
                <Route path='/score' component={Score}/>
                <Route exact path='/role' component={Role}/>
                <Route path='/role/a_apply' component={A_Apply}/>
                <Route path='/role/b_apply' component={B_Apply}/>
                <Route path='/role/pm_apply' component={PM_Apply}/>
                <Route path='/authentication' component={Authentication}/>
                <Route path='/expert/apply' component={ExpertApply}/>
                <Route path='/search' component={Search}/>
                <Route path='/new-project/:project_id?' component={NewProject} />
                <Route exact path='/assign' component={Assign} />
                <Route path='/assign/add' component={AddAssign} />
                <Route path='/add-schedule' component={AddSchedule} />
                <Route path='/set-milestone' component={SetMileStone} />
                <Route path='/add-revision/:project_id' component={NewRevision} />
                <Route path='/milestone/:project_id' component={Milestone} />
                <Route path='/profile/:user_id' component={Profile} />
                <Route path='/public-detail' component={PublicDetail} />
                
            </React.Fragment> 
                                    
        );
    }
}
// const WithShareWrapSwitch = wechatShareHOC(WrapSwitch)


function mapStateToProps(state) {
    return {
      shouldRender: state.globalState.shouldRender,
      userInfo: state.user.userInfo,
      isWechat: state.globalState.isWechat
    }
}
  
function mapDispatchToProps(dispatch) {
    return  {
      get_user_info: bindActionCreators(UserAction.get_user_info, dispatch),
      user_auth: bindActionCreators(actions.user_auth, dispatch)
    }
}
  
export default connect(
    mapStateToProps,
    mapDispatchToProps
)(App)